<style>
    .layuimini-container .layui-table-cell {
        height: auto;
    }
</style>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="log">日志</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            elem: '#currentTableId',
            url: '{:url("cron/index")}',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox"},
                {field: 'id', width: 60, title: 'ID'},
                {field: 'title', title: '任务名称'},
                {
                    field: 'type', minWidth: 80, title: '任务类型', templet: function (d) {
                        if (d.type == 'shell') {
                            return 'Shell脚本';
                        } else {
                            return '访问url';
                        }
                    }
                },
                {field: 'content', title: '任务详情'},
                {field: 'schedule', title: 'Cron表达式',minWidth: 100},
                {
                    title: '执行信息', templet: function (d) {
                        return ` <p>创建：` + d.create_time + `</p>
                            <p>更新：` + d.update_time + `</p>
                            <p>开始：` + d.begin_time + `</p>
                            <p>结束：` + d.end_time + `</p>
                            <p>最大执行次数：<span style="font-weight:bold;color:red;">` + d.maximums + `</span></p>
                            <p>已经执行次数：<span style="font-weight:bold;color:blue;">` + d.executes + `</span></p>
                            <p>最后执行：` + d.execute_time + `</p>`;
                    }
                },
                {
                    field: 'status', align: "center",title: '状态', width:80, templet: function (d) {
                        $str = '';
                        switch (parseInt(d.status)) {
                            case -1 :
                                $str = '<span class="label label-danger">已删除</span>';
                                break;
                            case 1 :
                                $str = '<span style="color: #01AAED;">正常</span>';
                                break;
                            case 0 :
                                $str = '<span style="color: #FF5722">禁用</span>';
                                break;
                            case 2 :
                                $str = '<span style="color: #5FB878">完成</span>';
                                break;
                            case 3 :
                                $str = '<span style="color: #FFB800">已过期</span>';
                                break;
                        }
                        return $str;
                    }
                },
                {title: '操作', minWidth: 180, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var event = obj.event;
            if (event === 'add') {  // 监听添加操作
                openDetail('新增', '{:url("cron/add")}');
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var ids = [];
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i]['id']);
                }
                delItem('{:url("cron/del")}', ids);
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                var url = '{:url("cron/edit",["id"=>"ID"])}';
                url = url.replace("ID", data.id);
                openDetail('修改', url);
            } else if (layEvent === 'delete') {
                delItem('{:url("cron/del")}', data.id);
            }else if(layEvent==='log'){
                var url = '{:url("cron/log",["crontab_id"=>"ID"])}';
                url = url.replace("ID",data.id);
                openDetail('执行日志',url);
            }
        });
    });
</script>